/**
 *  tool.js
 *  封装的公有方法
 * */

import _ from 'lodash'
import util from '_util/index';

const PRE = 'tz_vue_';
var in_array = require('in_array');

export default{
    lfetch(k){
        /**
         * local storage 读取
         * 符合tz_vue_*的数据
         * */
        k = PRE + k
        var items = window.localStorage.getItem(k) || "{}";
        return JSON.parse(items);
    },
    lsave(k, v){
        /**
         * local storage 存储
         * 符合tz_vue_*的数据
         * */
        k = PRE + k
        return window.localStorage.setItem(k, JSON.stringify(v));
    },
    fget(k){

    },
    cl(){
        for (let i = 0; i < arguments.length; i++) {
            console.log(arguments[i])
        }
    },
    getUrl(ctl, type, op){
        op = op || 'index';
        let pre = 'http://localhost:8080/user_center'
        let url = `${pre}/admin.php?s=/${ctl}/${op}/token/9a33ed3de10be659617aaec29a8e26cf/is_vue/1/${type}/1`
        return url;
    },
    getLines (str) {
        let fg='\n'
        str=_.trim(str, fg);
        let arr=_.split(str, fg);
        return arr;
    },
    nullToSpace (v) {
        return !v && v != '0' ? '' : v;
    },
    dateFormat(date,fmt){
        /**
         *  日期格式化
         *  @param {time} date:时间戳
         *  @param {string} fmt:匹配字符串,其中 y:年，M：月，d：日，h：小时，m：分，S: 毫秒，s：秒，q:季度
         *  @return {string} 格式化后的时间
         * */
        let o = {
            "M+": date.getMonth() + 1,
            "d+": date.getDate(),
            "h+": date.getHours(),
            "m+": date.getMinutes(),
            "s+": date.getSeconds(),
            "q+": Math.floor((date.getMonth() + 3) / 3),
            "S": date.getMilliseconds()
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (let k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    },
    getApi(_this, ctl, op = 'index', pre = 'index'){
        /**
         *  获取api
         *  pre：前缀
         *  ctl：控制器名
         *  op：接口名
         * */
        if (pre !== 'index') {
            pre = `/${pre}/`;
            let url = `${pre}${ctl}/${op}`;
            return url;
        }

        pre = '/index/'
        let _pre = null;
        if (_this) {
            util.store.commit(_this,'getModelByCtl', ctl);
            let model = util.store.get(_this,'vuexRs')
            _pre = model ? model.urlpre : null;
            _pre = _pre ? `/${_pre}/` : null;
        }
        pre = _pre || pre

        // let url ='http://localhost:8020'+`${pre}${ctl}/${op}`
        let url = `${pre}${ctl}/${op}`

        return url;
    },


    n2e(num){
        let map = [
            'one',
            'two',
            'three',
            'four',
            'five',
            'six',
            'seven',
            'eight',
            'nine',
            'ten',
            'eleven',
            'twelve',
            'thirteen',
            'fourteen',
            'fifteen',
            'sixteen',
            'seventeen',
            'eighteen',
            'nineteen',
            'twenty'
        ]
        return map[num];
    },
    RS_OK: 1,
    ROW_NUM: 20,
    BIND_ROW_NUM: 10,
    PAGE_NUM: 5,
    IMGPRE: 'https://oss-cn-shanghai.aliyuncs.com/tuzuu-weixin/'

}
